package com.goldenKey.common.config;

import com.goldenKey.common.constant.Constant;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 用于配置跨源资源共享
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Value("${cors.allowedOrigins:http://localhost:8080}")
    private String allowedOrigins;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(allowedOrigins)  // 使用配置项
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .exposedHeaders(Constant.CAPTCHA_KEY)
                .allowCredentials(true)
                .allowedHeaders("*");
    }
}
